<?php

class Controller_Dires extends Controller_Dires_Base {

	function action_index() {
		$db_num = Session::instance()->get('dires_db', null);
		if ($db_num === null) {
			Session::instance()->set('dires_db', Kohana::config('predis.default.database'));
		}
		$patterns = Session::instance()->get('dires_patterns', array());
		if (empty($patterns) || empty($patterns[Session::instance()->get('dires_db')])) {
			$patterns[Session::instance()->get('dires_db')] = array();
		}
		$patterns = $patterns[Session::instance()->get('dires_db')];
		$this->template->content = View::factory('dires/index')->bind('patterns', $patterns);
	}


	function action_pattern_add() {
		if (Request::current()->method() == 'POST') {
			if (!empty($_POST['pattern'])) {
				$patterns = Session::instance()->get('dires_patterns', array());
				if (empty($patterns) || empty($patterns[Session::instance()->get('dires_db')])) {
					$patterns[Session::instance()->get('dires_db')] = array();
				}

				$patterns[Session::instance()->get('dires_db')][] = $_POST['pattern'];
				Session::instance()->set('dires_patterns', $patterns);
			}
		}
		Request::initial()->redirect(URL::site(Route::get('dires')->uri(array('controller'=>'dires', 'action'=>'index')), null, false));
	}


	function action_pattern_edit() {
		if (Request::current()->method() == 'POST') {
			if (!empty($_POST['pattern'])) {
				$patterns = Session::instance()->get('dires_patterns', array());
				if (empty($patterns) || empty($patterns[Session::instance()->get('dires_db')])) {
					$patterns[Session::instance()->get('dires_db')] = array();
				}

				$patterns[Session::instance()->get('dires_db')][] = $_POST['pattern'];
				Session::instance()->set('dires_patterns', $patterns);
			}
		}
		Request::initial()->redirect(URL::site(Route::get('dires')->uri(array('controller'=>'dires', 'action'=>'index')), null, false));
	}


	function action_pattern_expand() {
		$myreturn = array();
		$db = Predis::instance();
		$db_num = Session::instance()->get('dires_db');
		$db->select($db_num);

		if (!empty($_GET['p'])) {
			$keys = $db->keys($_GET['p']);
		} elseif (!empty($_GET['nopattern'])) {
			$patterns = Session::instance()->get('dires_patterns', array());
			if (empty($patterns) || empty($patterns[Session::instance()->get('dires_db')])) {
				$patterns[Session::instance()->get('dires_db')] = array();
			}

			// we want the patterns for the current database only
			$patterns = $patterns[Session::instance()->get('dires_db')];
			// redis globing to PCRE
			for ($i = 0, $temp_count = count($patterns); $i < $temp_count; $i++) {
				$patterns[$i] = preg_replace(array('/\./', '/(?<!\\\)\*/', '/(?<!\\\)\?/'), array('\.', '.*', '.?'), $patterns[$i]);
			}

			$keys = $db->keys('*');
			$matches = array();
			foreach ($patterns as $p) {
				$matches = array_merge($matches, preg_grep("/$p/", $keys));
			}
			$keys = array_diff($keys, $matches);
		}

		if (!empty($keys)) {
			sort($keys);
			foreach ($keys as $k) {
				$type = $db->type($k);
				$myreturn[] = array('type'=>$type, 'k'=>$k);
			}
		}

		$this->template->content = json_encode($myreturn);
	}


	function action_change_db() {
		if (Request::current()->method() == 'POST') {
			$db_num = (int)$_POST['db'];
			Session::instance()->set('dires_db', $db_num);
			Dires_Utils::set_page_message(I18n::get('Database changed'), Dires_Utils::PAGE_MESSAGE_OK);
		}
		Request::initial()->redirect(URL::site(Route::get('dires')->uri(array('controller'=>'dires', 'action'=>'index')), null, false));
	}
}
